package com.aruba.filter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * ColorFilter 颜色变换
 */
public class ColorMatrixColorFilterView extends View {
    private Bitmap bitmap;

    public ColorMatrixColorFilterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画原图
        Paint paint = new Paint();
        RectF rectF = new RectF(100, 100, 100 + bitmap.getWidth() / 2, 100+bitmap.getHeight() / 2);
        canvas.drawBitmap(bitmap, null, rectF, paint);

        //画设置ColorFilter效果后的图
        rectF = new RectF(600, 100, 600 +bitmap.getWidth() / 2, 100+bitmap.getHeight() / 2);
        
        //将alpha值变为原来的一半
//        float[] colorMatrix = new float[]{
//                1,0,0,0,0,
//                0,1,0,0,0,
//                0,0,1,0,0,
//                0,0,0,0.5f,0,
//        };
        
        //平移运算---将red通道加上100
//        float[] colorMatrix = new float[]{
//                1,0,0,0,100,
//                0,1,0,0,0,
//                0,0,1,0,0,
//                0,0,0,1f,0,
//        };
        
        // 反相效果--底片效果
//        float[] colorMatrix = new float[]{
//                -1,0,0,0,255,
//                0,-1,0,0,255,
//                0,0,-1,0,255,
//                0,0,0,1f,0,
//        };
        
        //缩放运算---乘法 -- 颜色增强
//        float[] colorMatrix = new float[]{
//                1.2f,0,0,0,0,
//                0,1.2f,0,0,0,
//                0,0,1.2f,0,0,
//                0,0,0,1,0,
//        };
        
        // 黑白照片
        // 去色原理：只要把R G B 三通道的色彩信息设置成一样，那么图像就会变成灰色，
        // 同时为了保证图像亮度不变，同一个通道里的R+G+B =1
//        float[] colorMatrix = new float[]{
//                0.213f, 0.715f,0.072f,0,0,
//                0.213f, 0.715f,0.072f,0,0,
//                0.213f, 0.715f,0.072f,0,0,
//                0,0,0,1,0,
//        };
        
        // 发色效果---（比如红色和绿色交换）
//        float[] colorMatrix = new float[]{
//                0,1,0,0,0,
//                1,0,0,0,0,
//                0,0,1,0,0,
//                0,0,0,1,0,
//        };
        
        // 复古效果
//        float[] colorMatrix = new float[]{
//                1/2f,1/2f,1/2f,0,0,
//                1/3f, 1/3f,1/3f,0,0,
//                1/4f,1/4f,1/4f,0,0,
//                0,0,0,1,0,
//        };
        
        // 颜色通道过滤
        float[] colorMatrix = new float[]{
                1,0,0,0,0,
                0,0,0,0,0,
                0,0,0,0,0,
                0,0,0,1,0,
        };
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, null, rectF, paint);
    }
}
